package com.hkts.beltweigher.master.controller;


import com.alibaba.fastjson.JSON;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.beltweigher.master.vo.RegularTimeVo;
import com.hkts.beltweigher.utils.CustomPageUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hkts.beltweigher.utils.Result;
import com.hkts.beltweigher.utils.ResultUtil;
import com.hkts.beltweigher.master.service.IRegularTimeService;
import com.hkts.beltweigher.master.entity.RegularTime;

/**
 * @author hml
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/order/regularTime")
public class RegularTimeController {
    private final Logger logger = LoggerFactory.getLogger(RegularTimeController.class);

    @Autowired
    public IRegularTimeService iRegularTimeService;

    /**
     * 分页查询数据
     *
     * @param page        分页信息
     * @param regularTime 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getRegularTimePageList")
    public Result<Object> getregularTimeList(Page<RegularTime> page, RegularTime regularTime) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iRegularTimeService.selectPage(page, regularTime)));
    }

    /**
     * 保存和修改公用的
     *
     * @param regularTimeVo 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/regularTimeSave")
    public Result<Object> regularTimeSave(@RequestBody RegularTimeVo regularTimeVo) {
        RegularTime selectByStatus = iRegularTimeService.selectByStatus();
        if (selectByStatus != null){
            return ResultUtil.error("已有定时任务,请勿重复添加!");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime dateTime = LocalDateTime.parse(regularTimeVo.getStartTime(), formatter);
        RegularTime regularTime = new RegularTime();
        regularTime.setStatus(0);
        regularTime.setCoefficientf(regularTimeVo.getCoefficientf());
        regularTime.setStartTime(dateTime);
        boolean b = iRegularTimeService.saveOrUpdate(regularTime);

        if (b){
            return ResultUtil.success("保存成功");
        }else {
            return ResultUtil.error("保存失败");
        }

    }

    /**
     * 保存和修改公用的
     *
     * @param regularTimeVo 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/regularTimeUpdate")
    public Result<Object> regularTimeUpdate(@RequestBody RegularTimeVo regularTimeVo) {
        RegularTime byId = iRegularTimeService.getById(regularTimeVo.getId());

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime dateTime = LocalDateTime.parse(regularTimeVo.getStartTime(), formatter);

        byId.setCoefficientf(regularTimeVo.getCoefficientf());
        byId.setStartTime(dateTime);
        boolean b = iRegularTimeService.saveOrUpdate(byId);

        if (b){
            return ResultUtil.success("修改成功");
        }else {
            return ResultUtil.error("修改失败");
        }

    }

    /**
     * 根据id获取对象
     *
     * @param  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/regularTimeGetByStatus")
    public Result<Object> regularTimeGetById() {
        RegularTime regularTime = iRegularTimeService.selectByStatus();
        if (regularTime != null){
            return ResultUtil.data(regularTime);
        }else {
            return ResultUtil.success("暂无数据");
        }
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/regularTimeDelete")
    public Result<Object> regularTimeDelete(String id) {
        iRegularTimeService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/regularTimeBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iRegularTimeService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

}
